﻿
#If Not (PocketPC OrElse Smartphone) Then

Imports Microsoft.VisualBasic.Devices

Public Module hardware

    Private _computer As Computer = Nothing

    Public Function computer() As Computer
        If _computer Is Nothing Then
            _computer = New Computer()
        End If

        Return _computer
    End Function

    Public Function availablePhysicalMemory() As UInt64
        Try
            Return computer().Info.AvailablePhysicalMemory
        Catch
            Return 0
        End Try
    End Function

    Public Function availableVirtualMemory() As UInt64
        Try
            Return computer().Info.AvailableVirtualMemory
        Catch
            Return 0
        End Try
    End Function

    Public Function totalPhysicalMemory() As UInt64
        Return computer().Info.TotalPhysicalMemory
    End Function

    Public Function totalVirtualMemory() As UInt64
        Return computer().Info.TotalVirtualMemory
    End Function

    Private _processorArchitecture As String = Nothing
    Public Function processorArchitecture() As String
        If _processorArchitecture Is Nothing Then
            _processorArchitecture = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
        End If
        Return _processorArchitecture
    End Function

    Public Function osfullname() As String
        Return computer().Info.OSFullName()
    End Function

    Public Function osplatform() As String
        Return computer().Info.OSPlatform()
    End Function

    Public Function osversion() As String
        Return computer().Info.OSVersion()
    End Function

    Private _computername As String = Nothing
    Public Function computername() As String
        If _computername Is Nothing Then
            _computername = computer().Name()
        End If

        Return _computername
    End Function

    Private _machinename As String = Nothing
    Public Function machinename() As String
        If _machinename Is Nothing Then
            _machinename = Environment.MachineName()
        End If

        Return _machinename
    End Function

End Module

#End If

Public Module _perfcounter
    Public Function readPerformanceCounterValue(ByVal categoryName As String, _
                                                ByVal counterName As String, _
                                                ByVal instanceName As String, _
                                                ByRef output As Single, _
                                                Optional ByVal roundCount As Int32 = 32) As Boolean
        Static perfcounter As PerformanceCounter = New PerformanceCounter()
        If roundCount <= 0 Then
            roundCount = 32
        End If
        perfcounter.CategoryName = categoryName
        perfcounter.CounterName = counterName
        perfcounter.InstanceName = instanceName

        perfcounter.NextValue()
        sleep(128)
        Do
            output = perfcounter.NextValue()
            roundCount -= 1
            sleep(128)
        Loop Until output > 0 OrElse roundCount <= 0

        Return roundCount > 0
    End Function
End Module
